package com.gangquan360.smartadmin.module.product.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.product.dao.ProductCategoryDao;
import com.gangquan360.smartadmin.module.product.dao.ProductDao;
import com.gangquan360.smartadmin.module.product.domain.dto.ProductDTO;
import com.gangquan360.smartadmin.module.product.domain.dto.ProductQueryDTO;
import com.gangquan360.smartadmin.module.product.domain.entity.ProductCategoryEntity;
import com.gangquan360.smartadmin.module.product.domain.entity.ProductEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 产品表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-14 21:29:23
 * @since JDK1.8
 */
@Service
public class ProductService {

    @Autowired
    private ProductDao productDao;
    
    @Autowired
    private ProductCategoryDao productCategoryDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-14 21:29:23
     */
    public ResponseDTO<PageResultDTO<ProductDTO>> queryByPage(ProductQueryDTO queryDTO) {
        Page<ProductDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<ProductEntity> entities = productDao.queryByPage(page, queryDTO);
        List<ProductDTO> dtoList = SmartBeanUtil.copyList(entities, ProductDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<ProductDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-14 21:29:23
     */
    public ResponseDTO<String> add(ProductDTO addDTO) {
        ProductEntity entity = SmartBeanUtil.copy(addDTO, ProductEntity.class);
        productDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-14 21:29:23
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(ProductDTO updateDTO) {
        ProductEntity entity = SmartBeanUtil.copy(updateDTO, ProductEntity.class);
        productDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-14 21:29:23
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        productDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-14 21:29:23
    */
    public ResponseDTO<ProductDTO> detail(Long id){
        ProductEntity entity = productDao.selectById(id);
        ProductDTO dto =  SmartBeanUtil.copy(entity,ProductDTO.class);
        // 获取对应分类path
        Integer categoryId = dto.getCategory();
        String categoryPath = "";
        if (categoryId != null && categoryId != 0) {
        	ProductCategoryEntity cateEntity = productCategoryDao.selectById(categoryId);
        	if (cateEntity != null) {
        		categoryPath = cateEntity.getPath() + "," + categoryId;
        	}
        }
        dto.setCategoryPath(categoryPath);
        return ResponseDTO.succData(dto);
    }
}
